package com.ckjava.test.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ckjava.xutils.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class HttpResponseUtils {

    private static SerializerFeature[] jsonFeature = { SerializerFeature.WriteMapNullValue };
    private static Logger logger = LoggerFactory.getLogger(HttpResponseUtils.class);

    /**
     * 处理响应
     * @param response {@code HttpServletResponse} 对象
     * @param obj 需要处理的对的
     */
    public static void writeJsonResponse(HttpServletResponse response, Object obj) {
        try {
            response.setContentType("application/json;charset=UTF-8");
            response.getWriter().print(JSON.toJSONStringWithDateFormat(obj, Constants.TIMEFORMAT.DATETIME, jsonFeature));
        } catch (IOException e) {
            logger.error("AopOpLog processResponse has error", e);
        }
    }
}